<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.17"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>cpp-any: cpp-any</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">cpp-any
   </div>
   <div id="projectbrief">Implementation of std::any class of C++17 standard under C++11 standard</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.17 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

</div><!-- top -->
<div class="PageDoc"><div class="header">
  <div class="headertitle">
<div class="title">cpp-any </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><h3><a class="anchor" id="autotoc_md1"></a>
介绍</h3>
<p>C++11标准下实现C++17标准any类。</p>
<p>std::any是C++17标准才实现的类。若编译器不支持C++17标准，而我们又需要使用any类，cpp-any提供了一个实现的参考，接口与std::any保持一致。</p>
<h3><a class="anchor" id="autotoc_md2"></a>
要求</h3>
<p>编译器要求至少支持C++11标准。</p>
<h3><a class="anchor" id="autotoc_md3"></a>
Example</h3>
<ul>
<li>Normal usage</li>
</ul>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="any_8h.html">any.h</a>&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span>, <span class="keywordtype">char</span>*[])</div>
<div class="line">{</div>
<div class="line">    std::cout &lt;&lt; std::boolalpha;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">// any type</span></div>
<div class="line">    <a class="code" href="classany.html">any</a> a = 1;</div>
<div class="line">    std::cout &lt;&lt; a.type().name() &lt;&lt; <span class="stringliteral">&quot;: &quot;</span> &lt;&lt; any_cast&lt;int&gt;(a) &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div>
<div class="line">    a = 3.14;</div>
<div class="line">    std::cout &lt;&lt; a.type().name() &lt;&lt; <span class="stringliteral">&quot;: &quot;</span> &lt;&lt; any_cast&lt;double&gt;(a) &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div>
<div class="line">    a = <span class="keyword">true</span>;</div>
<div class="line">    std::cout &lt;&lt; a.type().name() &lt;&lt; <span class="stringliteral">&quot;: &quot;</span> &lt;&lt; any_cast&lt;bool&gt;(a) &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">// bad cast</span></div>
<div class="line">    <span class="keywordflow">try</span> {</div>
<div class="line">        a = 1;</div>
<div class="line">        std::cout &lt;&lt; any_cast&lt;float&gt;(a) &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div>
<div class="line">    } <span class="keywordflow">catch</span> (<span class="keyword">const</span> <a class="code" href="classbad__any__cast.html">bad_any_cast</a>&amp; e) {</div>
<div class="line">        std::cout &lt;&lt; e.<a class="code" href="classbad__any__cast.html#a2d2581230d8c46e3d861da95fcdb3f2c">what</a>() &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div>
<div class="line">    }</div>
<div class="line"> </div>
<div class="line">    <span class="comment">// has value</span></div>
<div class="line">    a = 1;</div>
<div class="line">    <span class="keywordflow">if</span> (a.<a class="code" href="classany.html#aee1aabbbe7c8e3197605c0f8055e5e5a">has_value</a>()) {</div>
<div class="line">        std::cout &lt;&lt; a.type().name() &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div>
<div class="line">    }</div>
<div class="line"> </div>
<div class="line">    <span class="comment">// reset</span></div>
<div class="line">    a.<a class="code" href="classany.html#a6c4a11253f803d135685e115cf7054d8">reset</a>();</div>
<div class="line">    <span class="keywordflow">if</span> (!a.<a class="code" href="classany.html#aee1aabbbe7c8e3197605c0f8055e5e5a">has_value</a>()) {</div>
<div class="line">        std::cout &lt;&lt; <span class="stringliteral">&quot;no value\n&quot;</span>;</div>
<div class="line">    }</div>
<div class="line"> </div>
<div class="line">    <span class="comment">// pointer to contained data</span></div>
<div class="line">    a = 1;</div>
<div class="line">    <span class="keywordtype">int</span>* i = any_cast&lt;int&gt;(&amp;a);</div>
<div class="line">    std::cout &lt;&lt; *i &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div>
<div class="line"> </div>
<div class="line">    <span class="keywordflow">return</span> 0;</div>
<div class="line">}</div>
</div><!-- fragment --><p>Possible output: </p><div class="fragment"><div class="line">i: 1</div>
<div class="line">d: 3.14</div>
<div class="line">b: true</div>
<div class="line">bad any_cast</div>
<div class="line">i</div>
<div class="line">no value</div>
<div class="line">1 </div>
</div><!-- fragment --><ul>
<li>Custom data structure</li>
</ul>
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="any_8h.html">any.h</a>&quot;</span></div>
<div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="keyword">struct </span>Point</div>
<div class="line">{</div>
<div class="line">    <span class="keywordtype">double</span> x{ 0.0 };</div>
<div class="line">    <span class="keywordtype">double</span> y{ 0.0 };</div>
<div class="line">};</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>* argv[])</div>
<div class="line">{</div>
<div class="line">    <a class="code" href="classany.html">any</a> a = Point{ 1.0, 2.0 };</div>
<div class="line"> </div>
<div class="line">    Point pt = any_cast&lt;Point&gt;(a);</div>
<div class="line">    std::cout &lt;&lt; pt.x &lt;&lt; std::endl;</div>
<div class="line">    std::cout &lt;&lt; pt.y &lt;&lt; std::endl;</div>
<div class="line"> </div>
<div class="line">    <span class="keywordflow">return</span> 0;</div>
<div class="line">}</div>
</div><!-- fragment --><p>Possible output: </p><div class="fragment"><div class="line">1</div>
<div class="line">2</div>
</div><!-- fragment --><h3><a class="anchor" id="autotoc_md4"></a>
Compatible</h3>
<div class="fragment"><div class="line"> </div>
<div class="line"><span class="preprocessor">#ifdef _MSVC_LANG</span></div>
<div class="line"><span class="preprocessor">#   define CPLUSPLUS__      _MSVC_LANG</span></div>
<div class="line"><span class="preprocessor">#else</span></div>
<div class="line"><span class="preprocessor">#   define CPLUSPLUS__      __cplusplus</span></div>
<div class="line"><span class="preprocessor">#endif </span><span class="comment">/* _MSVC_LANG */</span><span class="preprocessor"></span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#if CPLUSPLUS__ &gt; 201402L</span></div>
<div class="line"><span class="preprocessor">#   if (defined(_MSC_VER) &amp;&amp; _MSC_VER &gt;= 1910) || \</span></div>
<div class="line"><span class="preprocessor">       (defined(__GNUC__) &amp;&amp; __GNUC__ &gt;= 7) || \</span></div>
<div class="line"><span class="preprocessor">       (defined(__clang__) &amp;&amp; __clang_major__ &gt;= 4)</span></div>
<div class="line"><span class="comment">/* std::any */</span></div>
<div class="line"><span class="preprocessor">#       define HAS_STDANY__</span></div>
<div class="line"><span class="preprocessor">#endif</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#if defined(HAS_STDANY__)</span></div>
<div class="line"><span class="preprocessor">#   include &lt;any&gt;</span></div>
<div class="line"><span class="preprocessor">#else</span></div>
<div class="line"><span class="preprocessor">#   include &quot;<a class="code" href="any_8h.html">any.h</a>&quot;</span></div>
<div class="line"><span class="preprocessor">#endif</span></div>
<div class="line"> </div>
<div class="line"><span class="keyword">namespace </span>user_ns {</div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#if defined(HAS_STDANY__)</span></div>
<div class="line"> </div>
<div class="line"><span class="keyword">using</span> user_any = std::any;</div>
<div class="line"><span class="keyword">using</span> std::bad_any_cast;</div>
<div class="line"><span class="keyword">using</span> <a class="code" href="any_8h.html#a7686efd7fbdd733ca3fe088e719c0da8">std::any_cast</a>;</div>
<div class="line"><span class="keyword">using</span> <a class="code" href="any_8h.html#af597db5576048a3aefd30ec700d36339">std::make_any</a>;</div>
<div class="line"><span class="keyword">using</span> <a class="code" href="any_8h.html#ace81d9d9deec1c1fbd711a898990759f">std::swap</a>;</div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#else</span></div>
<div class="line"> </div>
<div class="line"><span class="keyword">using</span> user_any = <a class="code" href="classany.html">any</a>;</div>
<div class="line"><span class="keyword">using</span> <a class="code" href="classbad__any__cast.html">bad_any_cast</a>;</div>
<div class="line"><span class="keyword">using</span> <a class="code" href="any_8h.html#a7686efd7fbdd733ca3fe088e719c0da8">any_cast</a>;</div>
<div class="line"><span class="keyword">using</span> <a class="code" href="any_8h.html#af597db5576048a3aefd30ec700d36339">make_any</a>;</div>
<div class="line"><span class="keyword">using</span> <a class="code" href="any_8h.html#ace81d9d9deec1c1fbd711a898990759f">swap</a>;</div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#endif // !has std::any.</span></div>
<div class="line"> </div>
<div class="line">} <span class="comment">// namespace dcmnet</span></div>
</div><!-- fragment --><h3><a class="anchor" id="autotoc_md5"></a>
License</h3>
<div class="fragment"><div class="line">MIT License</div>
<div class="line"> </div>
<div class="line">Copyright (c) 2021 shaoguang</div>
<div class="line"> </div>
<div class="line">Permission is hereby granted, free of charge, to any person obtaining a copy</div>
<div class="line">of this software and associated documentation files (the &quot;Software&quot;), to deal</div>
<div class="line">in the Software without restriction, including without limitation the rights</div>
<div class="line">to use, copy, modify, merge, publish, distribute, sublicense, and/or sell</div>
<div class="line">copies of the Software, and to permit persons to whom the Software is</div>
<div class="line">furnished to do so, subject to the following conditions:</div>
<div class="line"> </div>
<div class="line">The above copyright notice and this permission notice shall be included in all</div>
<div class="line">copies or substantial portions of the Software.</div>
<div class="line"> </div>
<div class="line">THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR</div>
<div class="line">IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</div>
<div class="line">FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE</div>
<div class="line">AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</div>
<div class="line">LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,</div>
<div class="line">OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE</div>
<div class="line">SOFTWARE.</div>
</div><!-- fragment --> </div></div><!-- contents -->
</div><!-- PageDoc -->
<div class="ttc" id="aany_8h_html_af597db5576048a3aefd30ec700d36339"><div class="ttname"><a href="any_8h.html#af597db5576048a3aefd30ec700d36339">make_any</a></div><div class="ttdeci">any make_any(_Args &amp;&amp;... args)</div><div class="ttdef"><b>Definition:</b> <a href="any_8h_source.html#l00556">any.h:556</a></div></div>
<div class="ttc" id="aany_8h_html_a7686efd7fbdd733ca3fe088e719c0da8"><div class="ttname"><a href="any_8h.html#a7686efd7fbdd733ca3fe088e719c0da8">any_cast</a></div><div class="ttdeci">_ValueType any_cast(const any &amp;operand)</div><div class="ttdef"><b>Definition:</b> <a href="any_8h_source.html#l00586">any.h:586</a></div></div>
<div class="ttc" id="aany_8h_html"><div class="ttname"><a href="any_8h.html">any.h</a></div></div>
<div class="ttc" id="aany_8h_html_ace81d9d9deec1c1fbd711a898990759f"><div class="ttname"><a href="any_8h.html#ace81d9d9deec1c1fbd711a898990759f">swap</a></div><div class="ttdeci">void swap(any &amp;lhs, any &amp;rhs) noexcept</div><div class="ttdef"><b>Definition:</b> <a href="any_8h_source.html#l00545">any.h:545</a></div></div>
<div class="ttc" id="aclassbad__any__cast_html_a2d2581230d8c46e3d861da95fcdb3f2c"><div class="ttname"><a href="classbad__any__cast.html#a2d2581230d8c46e3d861da95fcdb3f2c">bad_any_cast::what</a></div><div class="ttdeci">virtual const char * what() const noexcept</div><div class="ttdef"><b>Definition:</b> <a href="any_8cpp_source.html#l00007">any.cpp:7</a></div></div>
<div class="ttc" id="aclassany_html"><div class="ttname"><a href="classany.html">any</a></div><div class="ttdoc">The class any describes a type-safe container for single values of any type.</div><div class="ttdef"><b>Definition:</b> <a href="any_8h_source.html#l00159">any.h:159</a></div></div>
<div class="ttc" id="aclassbad__any__cast_html"><div class="ttname"><a href="classbad__any__cast.html">bad_any_cast</a></div><div class="ttdoc">Exception thrown by the value-returning forms of any_cast on a type mismatch.</div><div class="ttdef"><b>Definition:</b> <a href="any_8h_source.html#l00133">any.h:133</a></div></div>
<div class="ttc" id="aclassany_html_a6c4a11253f803d135685e115cf7054d8"><div class="ttname"><a href="classany.html#a6c4a11253f803d135685e115cf7054d8">any::reset</a></div><div class="ttdeci">void reset() noexcept</div><div class="ttdef"><b>Definition:</b> <a href="any_8cpp_source.html#l00066">any.cpp:66</a></div></div>
<div class="ttc" id="aclassany_html_aee1aabbbe7c8e3197605c0f8055e5e5a"><div class="ttname"><a href="classany.html#aee1aabbbe7c8e3197605c0f8055e5e5a">any::has_value</a></div><div class="ttdeci">bool has_value() const noexcept</div><div class="ttdef"><b>Definition:</b> <a href="any_8cpp_source.html#l00103">any.cpp:103</a></div></div>
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.17
</small></address>
</body>
</html>
